// pages/mine/regist/testphonenumber/identifyingcode/setpsw/binding/binding.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    focus: true,
    phoneNum:'',
    psw: '',
    currentState: -1 // -1 未输入状态， 0 输入错误， 1 输入正确且满八位 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options', options)
    let self = this
    self.setData({
      phoneNum: options.phoneNum,
      psw: options.psw
    })
    self.testPsw()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 输入手机号
   */
  phoneInput: function (value) {
    let self = this
    console.log('value', value)
    self.data.phoneNum = value.detail.value
    // 清除数字以外的任意字符
    let reg = /[^\d]/g
    if (reg.test(self.data.phoneNum)) {
      self.data.phoneNum = value.detail.value.replace(reg, '')
    }
  },
  /**
   * 检验输入的号码格式
   */
  checkPhoneNum: function (data, role) {
    let self = this
    let error = null
    let reg = /^1[3|4|5|7|8][0-9]{9}$/g
    let regPsw = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/g
    if (role === 0 && data === '') {
      error = '请输入手机号'
      return error
    }
    if (role === 1 && data === '') {
      error = '请输入密码'
      return error
    }
    if (role === 0 && !reg.test(data)) {
      error = '手机号输入有误'
      return error
    }
    if (role === 1 && !regPsw.test(data)) {
      error = '密码输入有误'
      return error
    }
    return error
  },
  /**
   * 输入密码
   */
  pswInput: function (event) {
    let self = this
    let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/g
    if (!event.detail.value) {
      this.data.psw = ''
      self.setData({
        currentState: -1,
      })
    } else if (reg.test(event.detail.value)) {
      this.data.psw = event.detail.value
      self.setData({
        currentState: 1
      })
    } else {
      this.data.psw = event.detail.value
      self.setData({
        currentState: 0
      })
    }
  },
  /**
   * 验证已有密码
   */
  testPsw: function () {
    let self = this
    let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/g
    if (!self.data.psw) {
      self.setData({
        currentState: -1,
      })
    } else if (reg.test(self.data.psw)) {
      self.setData({
        currentState: 1
      })
    } else {
      self.setData({
        currentState: 0
      })
    }
  },
  /**
   * 下一步
   */
  nextStep: function () {
    let self = this
    let errorPhone = self.checkPhoneNum(self.data.phoneNum, 0)
    let errorPsw = self.checkPhoneNum(self.data.psw, 1)
    if (errorPhone) {
      wx.showToast({
        title: errorPhone,
        image: '../../../../../../../resource/icon/notice@2x.png',
        duration: 2000
      })
      return
    }
    if (errorPsw) {
      wx.showToast({
        title: errorPsw,
        image: '../../../../../../../resource/icon/notice@2x.png',
        duration: 2000
      })
      return
    }
    var insApp = getApp()
    var params = {
      code: insApp.globalData.code,
      type: 1
    }
    var postparams = {
      'userName': self.data.phoneNum,
      'password': self.data.psw
    }
    wx.showLoading({
      title:'正在加载...',
      mask:true
    })
    insApp.login(postparams).then((res) => {
      console.log('res用户信息-------', res)
      wx.request({
        url: wx.envConfig.host + '/user/bindWxOpenid?params=' + JSON.stringify(params) + '&cookiez=' + res.cookiez,
          method: 'GET',
          success: function (res) {
            wx.hideLoading()
            console.log('res绑定回调', res)
            if (res && res.data.error_code === 'Y10000') {
              wx.switchTab({
                url: '../../../../../../home/main/main',
              })
            } else {
              wx.hideLoading()
              wx.showToast({
                title: '密码输入有误',
                image:'../../../../../../../resource/icon/notice@2x.png',
                duration: 2000
              })
            }
          },
          fail: function (err) {
            // 请求错误提示
            wx.hideLoading()
            console.log('err', err)
          }
        })
    })
  }
})